﻿using System;

namespace EarthLineTransmitter
{
    //1.地线控制器未启动，禁止五防子站接入；
    //2.地线控制器上传的数据，要转发到五防子站；
    //3.五防子站上传的数据，要转发到地线控制器；
    public class EarthLineController
    {
        public EarthLineController()
        {
        }

        public void start()
        {
            if (_started)
            {
                return;
            }
            _clientSendPool = new ClientSendPool();
            _sendService = new SendService(_clientSendPool);
            _sendService.Start();

            _wf = new WfServer();
            _wf.Name = "WF";
            int wfPort = 6000;
            _wf.Port = wfPort;
            _wf.OnData += _wf_OnData;
            //_wf.start();
            //_wf.idle();

            _earthLine = new EarthLineServer();
            _earthLine.Name = "EarthLine";
            int earthLinePort = 7000;
            _earthLine.Port = earthLinePort;
            _earthLine.OnTurnOn += _earthLine_OnTurnOn;
            _earthLine.OnTurnOff += _earthLine_OnTurnOff;
            _earthLine.OnData += _earthLine_OnData;
            _earthLine.start();

            _started = true;
        }

        public void stop()
        {

            _wf.stop();
            _earthLine.stop();
            _sendService.Stop();

            _wf = null;
            _earthLine = null;

            _started = false;
        }

        //3
        private void _wf_OnData(object sender, EventArgs e)
        {
            Console.WriteLine("wf on data ++++++ 1");
            //Console.WriteLine("[{0}][Data]", DateTime.Now.ToString("HH:mm:ss:fff"));
            //_earthLine.transmit(e);
            if (!_started)
            {
                return;
            }
            Console.WriteLine("wf on data ++++++ 2");
            SendInfo info = new SendInfo(_earthLine, e);
            lock (_lock)
            {
                Console.WriteLine("wf on data ++++++ 4");
                _clientSendPool.Enqueue(info);
            }
            Console.WriteLine("wf on data ++++++ 4");
        }
        //3

        //2
        private void _earthLine_OnData(object sender, EventArgs e)
        {
            Console.WriteLine("dx on data ++++++ 1");
            //_wf.transmit(e);
            if (!_started)
            {
                return;
            }
            Console.WriteLine("dx on data ++++++ 2");
            SendInfo info = new SendInfo(_wf, e);
            lock (_lock)
            {
                Console.WriteLine("dx on data ++++++ 3");
                _clientSendPool.Enqueue(info);
            }
            Console.WriteLine("dx on data ++++++ 4");
        }
        //2

        //1
        private void _earthLine_OnTurnOff(object sender, EventArgs e)
        {
            _wf.stop();
        }

        private void _earthLine_OnTurnOn(object sender, EventArgs e)
        {
            _wf.start();
        }
        //1

        private WfServer _wf = null;
        private EarthLineServer _earthLine = null;
        private bool _started = false;
        private SendService _sendService;
        private ClientSendPool _clientSendPool;
        private object _lock = new object();
    }
}